<template>
    <div>
        <van-popup :value="isShow" position="bottom" :style="{ height: '100%' }">
            <van-nav-bar title="登录" right-text="取消" @click-right="onClick" />
            <div class="phone-login-box">
                <div class="logo">{{ title }}</div>
                <el-form :model="form" :rules="rules" ref="form" label-position="top">
                    <el-form-item label="用户名" prop="username">
                        <el-input size="small" v-model="form.username" auto-complete="off" clearable
                            placeholder="用户名/邮箱"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password" v-if="!isCode">
                        <el-input size="small" type="password" v-model="form.password" show-password auto-complete="off"
                            placeholder="请输入密码"></el-input>
                    </el-form-item>
                    <el-form-item label="验证码" prop="code" v-if="isCode">
                        <el-input size="small" v-model="form.code" auto-complete="off" placeholder="请输入验证码">
                            <template slot="append">
                                <el-button type="text" size="small" @click="getCode" :disabled="timeScend != 60">{{
                                    timeScend !=
                                    60 ? timeScend + 's后重试' : '获取验证码' }}</el-button>
                            </template>
                        </el-input>
                    </el-form-item>
                    <el-form-item class="btn-box">
                        <el-checkbox size="mini" :disabled="isCode" v-model="isRemember"
                            @change="rememberPwd">记住密码</el-checkbox>
                        <el-button type="text" size="mini" @click="forgetPwd">忘记密码</el-button>
                    </el-form-item>
                    <el-form-item class="btn-submit-box">
                        <el-button size="small" type="primary" round @click="submitForm('form')">登录</el-button>
                    </el-form-item>
                    
                    <el-form-item class="btn-box">
                        <el-button type="text" size="mini" @click="signUp">还没账号？注册</el-button>
                        <el-button type="text" size="mini" @click="isCode = !isCode">
                            {{ isCode ? '密码登录' : '验证码登录' }}
                        </el-button>
                    </el-form-item>
                </el-form>
            </div>
        </van-popup>
    </div>
</template>
<script>
export default {
    name: 'phone-login',
    props: {
        isShow: {
            type: Boolean,
            default: false
        },
        timeScend: {
            type: Number,
            default: 60
        },
        title: {
            type: String,
            default: ''
        },
        rememberInfo: {
            type: Object,
            default: () => {
                return {
                    username: '',
                    password: ''
                }
            }
        }
    },
    data() {
        return {
            form: {
                username: '',
                password: '',
                code: ''
            },
            isCode: false,
            isRemember: false
        }
    },
    watch: {
        rememberInfo: {
            handler(val) {
                this.form.username = val.username;
                this.form.password = val.password;
                if (val.username && val.password) { this.isRemember = true; }
            },
            deep: true
        }
    },
    computed: {
        rules() {
            return {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' }
                ],
                password: [
                    { required: !this.isCode, message: '请输入密码', trigger: 'blur' }
                ],
                code: [
                    { required: this.isCode, message: '请输入验证码', trigger: 'blur' },
                    { min: 6, max: 6, message: '验证码长度为6', trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.$emit('submit', this.form);
                } else {
                    return false;
                }
            });
        },
        signUp() {
            this.$emit('signUp');
        },
        onClick() {
            this.$emit('close', false);
        },
        getCode() {
            this.$emit('getCode' , this.form.username);
        },
        /**记住密码*/
        rememberPwd() {
            this.$emit('rememberPwd', this.isRemember, this.form);
        },
        forgetPwd() {
            this.$emit('forgetPwd');
        }
    }
}
</script>
<style lang="scss" scoped>
.phone-login-box {
    padding: 20px 50px;
    position: fixed;
    top: 40%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 100%;

    .logo {
        font-size: 20px;
        text-align: center;
        margin-bottom: 20px;
    }

    /deep/.el-form-item {
        margin-bottom: 0px;

        .el-form-item__label {
            line-height: normal;
            padding-bottom: 5px;
        }

        .el-form-item__error {
            top: -23px;
            left: auto;
            right: 10px;
        }
    }

    /deep/.el-button--primary {
        background-color: var(--themeColor);
        border-color: var(--themeColor);
    }

    .btn-submit-box {
        text-align: center;

        .el-button {
            width: 100%;
        }
    }

    .btn-box {
        display: flex;
        justify-content: space-between;
    }

    .btn-box {

        // text-align: center;
        /deep/.el-form-item__content {
            display: flex;
            justify-content: space-between;
            align-items: center;
            width: 100%;

            &::before {
                display: none;
            }

            &::after {
                display: none;
            }

            .el-checkbox {
                .el-checkbox__label {
                    font-size: 12px;
                }
            }
        }
    }

}

/deep/.van-nav-bar__text {
    color: var(--themeColor);
}
</style>